import time


def cal_time(fn):
    print('cal_timing...')
    print('fn = {}'.format(fn))

    def inner():
        start = time.time()
        fn()
        end = time.time()
        print('cost {}s'.format(end - start))

    return inner


@cal_time
def func0():
    time.sleep(3)
    print('func0')


@cal_time
def func1():
    time.sleep(2)
    print('func1')


func0()
func1()
func0()


def cal_time2(fn):
    print('cal_tim2ing...')
    print('fn = {}'.format(fn))

    def inner():
        start = time.time()
        value = fn()
        end = time.time()
        print('cost {}s'.format(end - start))
        return value

    return inner


#  当函数有返回值时
@cal_time
def func3():
    return 'func3'


@cal_time2
def func4():
    return 'func4'


s1 = func3()
s2 = func4()
print('s1 = {}, s2 = {}'.format(s1, s2))  # s1 = None, s2 = func4


def cal_time3(fn):
    print('cal_tim3ing...')
    print('fn = {}'.format(fn))

    def inner(x, *args, **kwargs):  # *args, **kwargs使得inner的参数列表可以扩充
        start = time.time()
        value = fn(x)
        end = time.time()
        print('cost {}s'.format(end - start))
        return value, end - start

    return inner


@cal_time3
def func5(x):
    time.sleep(1)
    s = 0
    for i in range(0, x + 1):
        s += i
    return s


print(func5(100))  # 由于cal_time3中inner的参数列表扩充，使得此处func5（实际已经变成了inner）可以传入更多参数
